package com.amoby.main.action.channel;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amoby.base.common.BaseAction;
import com.amoby.base.common.Pager;
import com.amoby.main.entity.Channel;
import com.amoby.main.service.ChannelService;

/**
 * ParamAction Desc：渠道管理
 * @address http://www.yingmob.com/
 * @author zhaoyy
 * @date 2016年4月11日 上午10:44:37
 */
@Controller
public class ChannelAction extends BaseAction {
    @Autowired
    private ChannelService     channelService;
  
    /**
     * 初始化时间
     * @author Lasing 2016-6-3 上午10:55:32
     * @param binder
     * @throws Exception
     */
    @InitBinder
    protected void init(WebDataBinder binder) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
    /**
     * 初始化页面
     * @author Lasing 2016-6-3 上午10:55:40
     * @return
     */
    @RequestMapping(value = "/menus/channel_mgr.html", method = RequestMethod.GET)
    public String listPage(ModelMap model) {
        return AD_HTML + "channel/channel_mgr";
    }


    /**
     * 渠道分页查询
     * @author Lasing 2016-6-3 上午10:56:44
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/channel/list.html", method = RequestMethod.POST)
    public Object list(HttpServletRequest request) {
        Pager pager = createPager(request);
        pager.addParam("channelName", getRequestParams(String.class, request, "channelName"));
        pager.addParam("id", getRequestParams(String.class, request, "id"));
        pager.addParam("status", getRequestParams(String.class, request, "status"));
        pager = channelService.query(pager);
        return getGridData(pager);
    }

    
    /**
     * 渠道新增
     * @author Lasing 2016-6-3 上午10:57:02
     * @param business
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/channel/add.html", method = RequestMethod.POST)
    public Object add(Channel channel, HttpServletRequest request) {
        Map<String, Object> result = getResultMap();
        try {
            if (channel != null && channelService.add(channel) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "添加渠道成功！");
            } else {
                result.put(MESSAGE, "添加渠道失败！");
            }
        } catch (Exception e) {
            result.put(MESSAGE, "添加渠道异常！");
            getLog(this).error("添加渠道异常！", e);
        }
        return result;
    }

    /**
     * 渠道修改
     * @author Lasing 2016-6-3 上午10:57:12
     * @param business
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/channel/upd.html", method = RequestMethod.POST)
    public Object upd(Channel channel, HttpServletRequest request) {
        Map<String, Object> result = getResultMap();
        try {
            if (channel != null && channel.getId() != null && channelService.upd(channel) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "修改渠道成功！");
            } else {
                result.put(MESSAGE, "修改渠道失败！");
            }
        } catch (Exception e) {
            result.put(MESSAGE, "修改渠道异常！");
            getLog(this).error("修改渠道异常！", e);
        }
        return result;
    }

    /**
     * 渠道重置密码
     * @author Lasing 2016-6-3 上午10:57:29
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/channel/resetPassword.html", method = RequestMethod.POST)
    public Object reset_pwd(HttpServletRequest request) {
        Map<String, Object> result = getResultMap();
        try {
            Long id = getRequestParams(Long.class, request, "id");
            Integer userId = getRequestParams(Integer.class, request, "userId");
            if (id != null && channelService.reset_pwd(id, userId) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "重置渠道密码成功！");
            } else {
                result.put(MESSAGE, "重置渠道密码失败！");
            }
        } catch (Exception e) {
            result.put(MESSAGE, "重置渠道密码异常！");
            getLog(this).error("重置渠道密码异常！", e);
        }
        return result;
    }

    /**
     * 删除渠道
     * @author Lasing 2016-6-3 上午10:58:01
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/channel/del.html", method = RequestMethod.POST)
    public Object del(HttpServletRequest request) {
        Map<String, Object> result = getResultMap();
        try {
            Long id = getRequestParams(Long.class, request, "id");
            Integer userId = getRequestParams(Integer.class, request, "userId");
            if (id != null && channelService.del(id, userId) > 0 ) {
                result.put(RESULT, true);
                result.put(MESSAGE, "删除渠道成功！");
            } else {
                result.put(MESSAGE, "删除渠道失败！");
            }
        } catch (Exception e) {
            result.put(MESSAGE, "删除渠道异常！");
            getLog(this).error("删除渠道异常！", e);
        }
        return result;
    }

    /**
     * 渠道验证唯一性
     * @param username id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/channel/checkUnById.html", method = RequestMethod.POST)
    public Object checkUnById(String username, Long id, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (StringUtils.isBlank(username)) {
                result.put(RESULT, false);
                result.put(MESSAGE, "登陆名不能为空！");
            }
            int count = channelService.checkUserNameById(username, id);
            result.put(RESULT, true);
            result.put("count", count);
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "验证用户名异常！");
        }
        return result;
    }
    
    /**
     * 渠道验证唯一性
     * @param username id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/channel/checkChannelName.html", method = RequestMethod.POST)
    public Object checkChannelName(String channelName, Long id, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (StringUtils.isBlank(channelName)) {
                result.put(RESULT, false);
                result.put(MESSAGE, "渠道名称不能为空！");
            }
            int count = channelService.checkChannelName(channelName, id);
            result.put(RESULT, true);
            result.put("count", count);
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "验证渠道名称异常！");
        }
        return result;
    }
}
